#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

int main() {
    int server_fd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8888);
    addr.sin_addr.s_addr = INADDR_ANY;

    bind(server_fd, (struct sockaddr*)&addr, sizeof(addr));
    listen(server_fd, 5);

    printf("服务器即将启动，等待客户端连接...\n");

    struct sockaddr_in client;
    socklen_t len = sizeof(client);
    int client_fd = accept(server_fd, (struct sockaddr*)&client, &len);
    printf("客户端已连接：%s\n", inet_ntoa(client.sin_addr));

    char buf[1024];
    while (1) {
        ssize_t n = recv(client_fd, buf, sizeof(buf)-1, 0);
        if (n <= 0) break;
        buf[n] = '\0';
        printf("我是服务器，客户端说：%s\n", buf);
    }

    close(client_fd);
    close(server_fd);
    return 0;
}

